<?php

namespace app\admin\controller\general;

use app\common\controller\Backend;
use fast\Random;
use think\Db;
use think\Session;

/**
 * 店铺入驻管理
 *
 * @icon fa fa-circle-o
 */
class Storejoin extends Backend
{

    /**
     * Storejoin模型对象
     * @var \app\admin\model\Storejoin
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Storejoin;
        $this->view->assign("storeTypeList", $this->model->getStoreTypeList());
        $this->view->assign("storeStatusList", $this->model->getStoreStatusList());
        $this->view->assign("reasonIsSeeList", $this->model->getReasonIsSeeList());

        $this->view->assign("agreementList", $this->model->getAgreementList());
    }
    /**
     * 编辑
     */
    public function edit($ids = NULL)
    {
        $shop_id=Session::get('admin.id');
        $row = $this->model->get($shop_id);
        if (!$row)
            $this->error(__('No Results were found'));
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");

            if(!strstr($params['business_licence_number'],'http')){
                $params['business_licence_number']=SHOPURL.$params['business_licence_number'];
            }
            /*if(!strstr($params['qrcode'],'http')){
                $params['qrcode']=SHOPURL.$params['qrcode'];
            }*/
            if(!strstr($params['store_label'],'http')){
                $params['store_label']=SHOPURL.$params['store_label'];
            }

                $business_license=explode(',',$params['business_license']);
                $bus='';
                foreach ($business_license as $key=>$val){
                    if(!strstr($val,'http')) {
                        $business_license[$key] = SHOPURL . $val;
                    }
                }
                $params['business_license']=implode(',',$business_license);


            if ($params) {
                Db::startTrans();
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = basename(str_replace('\\', '/', get_class($this->model)));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
                        $row->validate($validate);
                    }
                    $result = $row->allowField(true)->save($params);
                    if ($result !== false) {
                       /* switch ($params['store_status']){
                            case 1:
                            case 5:
                            case 3:
                            case 4:
                                Db::connect(config('database.shop'))->name('admin')->update(['status'=>'hidden']);
                                break;
                            case 2:
                                Db::connect(config('database.shop'))->name('admin')->update(['status'=>'normal']);
                                break;
                        }*/
                        Db::commit();
                        $this->success();
                    } else {
                        Db::rollback();
                        $this->error($row->getError());
                    }
                } catch (\think\exception\PDOException $e) {
                    $this->error($e->getMessage());
                } catch (\think\Exception $e) {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $row['business_licence_number']=str_replace(SHOPURL,'',$row['business_licence_number']);
        $row['qrcode']=str_replace(SHOPURL,'',$row['qrcode']);
        $row['store_label']=str_replace(SHOPURL,'',$row['store_label']);
        $business_license=explode(',',$row['business_license']);
        $bus='';
        foreach ($business_license as $key=>$val){

           $business_license[$key]=str_replace(SHOPURL,'',$val);

        }

        $row['business_license']=implode(',',$business_license);

        $this->view->assign("row", $row);
        return $this->view->fetch();
    }
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    public function pass($ids=null){
        $ids=input('ids');
        //if ($this->request->isPost()) {
        Db::startTrans();
        try {
            $data['store_status']=2;
            $data['updatetime']=date('YmdHis');
//            $data['sell_name']='用户'.time();
            $data['sell_pwd']=md5('123456');
            $res = db('store_join')->where(['id' => $ids])->update($data);
            $shop_info=Db::name('store_join')->where(['id' => $ids])->find();
            #分配账号密码
            $salt=Random::alnum();
            $admin_data=[
                'id'=>$ids,
                'nickname'=>$shop_info['store_name'],
                'username'=>$shop_info['mobile'],
                'createtime'=>time(),
                'updatetime'=>time(),
                'salt' => $salt,
                'password' => md5(md5('123456') . $salt),
                'avatar' => '/assets/img/avatar.png', //设置新管理员默认头像。
            ];
            Db::connect(config('database.shop'))->name('admin')->insert($admin_data);
            if ($res) {
                Db::commit();
                $this->success('审核通过');
            } else {
                Db::rollback();
                $this->error('审核失败');
            }
            Db::rollback();
        } catch (\think\exception\PDOException $e) {
            $this->error($e->getMessage());
        } catch (\think\Exception $e) {
            $this->error($e->getMessage());
        }
        // }
    }
    public function refuse($ids=null){
        $ids=input('ids');
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if($params){
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = basename(str_replace('\\', '/', get_class($this->model)));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
                        $this->model->validate($validate);
                    }
                    $res=db('store_join')->where(['id'=>$ids])->update(['store_status'=>4,'store_reason'=>$params['store_reason'],'updatetime'=>date('YmdHis')]);
                    if ($res !== false) {
                        $this->success();
                    } else {
                        $this->error();
                    }
                } catch (\think\exception\PDOException $e) {
                    $this->error($e->getMessage());
                } catch (\think\Exception $e) {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }

        return $this->view->fetch();
    }


}
